<?php
defined('IN_MAIN') or exit('Invalid Access!');

function calc_rank($in_uid, $in_old_bv){
/*
This function calculate the member based on member personal volume in plan A (first sale = y), with achievement of rank
is based on the volume as below:

basic = 200bv
bronze = 400bv
silver = 1200bv
gold = 2800bv
platinum = 6000bv
*/

	//globalize access to rank_index_a_price
	global $vars;
	$db=$vars["db"];
	$rank_index_a_price = $vars['rank_index_a_price'];
	
	$user = get_user_detail_by_id($in_uid);
	$old_rank = doubleval($user['rank_a']);
	$vold_bv = $rank_index_a_price[$user['rank_a']];
	if($in_old_bv < $vold_bv){//means previous rank was earned by admin giving rank, no sales, add back virtual sales volume and recalculate rank
		$old_bv = $rank_index_a_price[$user['rank_a']];
	}else{
		$old_bv = 0;
	}
	//sales = get the total sales bv that is confirmed and is first sale of a member
	$total_bv = @mysql_result($r=mysql_query("select sum(total_bv) from $db->purchase where uid='$in_uid' and first_sale='y' and no_bv!='y' and status='confirmed'"), 0) or 0;
	$total_bv += $old_bv;
	$calc_rank = '0';
	$rank_i = array_reverse($rank_index_a_price, true);
	foreach($rank_i as $rank_index => $req_bv){
		if($total_bv >= $req_bv){
			$calc_rank = $rank_index;
			break;
		}
	}
	
	return $calc_rank;
}
?>